package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.AppTermUser;
import com.zjmzxfzhl.modules.app.service.AppDocService;
import com.zjmzxfzhl.modules.app.service.AppGitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppProjGit;
import com.zjmzxfzhl.modules.app.mapper.AppProjGitMapper;
import com.zjmzxfzhl.modules.app.service.AppProjGitService;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppProjGitServiceImpl extends BaseServiceImpl<AppProjGitMapper, AppProjGit>
        implements AppProjGitService {
    @Autowired
    private AppGitService appGitService;
    @Override
    public IPage<AppProjGit> list(IPage<AppProjGit> page, AppProjGit appProjGit) {
        return page.setRecords(baseMapper.list(page, appProjGit));
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProjGit
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProjGit(AppProjGit appProjGit) {
        List<AppProjGit> appTermUserList=baseMapper.list(null, appProjGit);
        if(appTermUserList.size()>0){
            return false;
        }
        Boolean result=this.save(appProjGit);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProjGit
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppProjGit appProjGit) {
        String gitId=appProjGit.getGitId();
        Map<String, Object> columnMap=new HashMap<>();
        columnMap.put("PROJ_ID",appProjGit.getProjId());
        columnMap.put("GIT_ID",appProjGit.getGitId());
        Boolean result=this.removeByMap(columnMap);

        AppProjGit fromProjGit=new AppProjGit();
        fromProjGit.setGitId(gitId);
        List<AppProjGit> fromProjGitList=baseMapper.list(null, fromProjGit);
        if(fromProjGitList.size()>0){
            return true;
        }else
        {
            appGitService.removeById(gitId);
        }
        return result;
    }
}
